
#include   <sys/types.h>
#include   <sys/socket.h>
#include   <string.h>
#include   <netinet/in.h>
#include   <stdio.h>
#include   <stdlib.h>
#include   <errno.h>

#define   MAXLINE   80
#define   SERV_PORT   8004

int   main()
{
	int   sockfd=0;
	socklen_t   len;
	char   mesg[MAXLINE];
	int   n;
	int   returecv=0;
	int   flag=0;

	struct   sockaddr_in   servaddr;
	char   buffercv_1[]= "regist_ok\n ";
	char   buffercv_2[]= "need_add_data\n ";


	sockfd   =   socket(AF_INET,   SOCK_DGRAM,   0);   /*   create   a   socket   */
	memset(   &servaddr,   0,   sizeof(struct   sockaddr)   );

	/*   init   servaddr   */
	bzero(&servaddr,   sizeof(servaddr));
	servaddr.sin_family   =   AF_INET;
	servaddr.sin_addr.s_addr   =   INADDR_ANY;
	servaddr.sin_port   =htons(SERV_PORT);
	bzero(&(servaddr.sin_zero),8);

	/*   bind   address   and   port   to   socket   */
	if(bind(sockfd,   (struct   sockaddr   *)&servaddr,   sizeof(struct   sockaddr))   ==   -1)
	{
		fprintf(stderr, "error:%s\n ",strerror(errno));
		exit(1);
	}
	for(;;)
	{
		len   =   sizeof(struct   sockaddr_in);
		/*   waiting   for   receive   data   */
		n   =   recvfrom(sockfd,   mesg,   MAXLINE,   0,   (struct   sockaddr   *)&servaddr,   &len);
		if(n==-1)
		{
			perror( "recv   error   server ");
			exit(1);
		}
		else
		{
			if(flag==0)
			{
				returecv=sendto(sockfd,   buffercv_1,   strlen(buffercv_1),   0,   (struct   sockaddr   *)&servaddr,   sizeof(struct   sockaddr));
				printf( "%s   %d\n ",buffercv_1,returecv);
				if(returecv==-1)
				{
					perror( "send_1   error ");
					exit(1);
				}
				flag=1;
			} 
			else if(flag==1)
			{
				returecv=sendto(sockfd,   buffercv_2,   strlen(buffercv_2),   0,   (struct   sockaddr   *)&servaddr,   sizeof(struct   sockaddr));
				//printf( "%s   %d\n ",buffercv_2,returecv);
				if(returecv==-1)
				{
					perror( "send_2   error ");
					exit(1);
				}
				flag=0;
			}

		}
	}
	return   0;
}

